/**
 * 
 */
package com.alstudio.net.EventHandler;

import com.alstudio.app.LocalEnv;
import com.alstudio.net.callback.XmppEventListener;
import com.alstudio.net.protocol.xmpp.data.XmppConnectionState;
import com.alstudio.net.protocol.xmpp.data.XmppEvent;
import com.alstudio.net.protocol.xmpp.packact.Message;
import com.alstudio.pojo.LocalData;
import com.alstudio.utils.log.Mylog;
import com.alstudio.view.toast.MyToastUtil;

/**
 * 此类用于监听网络层传递上来的xmpp事件，经过封装后，将事件通知到ui层
 * 
 * @author Alonso Lee
 * 
 */
public class XmppEventHandler implements XmppEventListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.net.callback.XmppEventListener#onConnectLostEvent(java.lang
	 * .String)
	 */
	@Override
	public void onConnectLostEvent(String reason) {
		// TODO Auto-generated method stub
		LocalData data = new LocalData(XmppEvent.XMPP_CONNECTION_LOST_EVENT);
		data.setErrorMessage(reason);
		LocalEnv.mXmppEventNotifyManager.notifyAllListener(data);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.net.callback.XmppEventListener#onLoginedEvent(java.lang.
	 * String, java.lang.String)
	 */
	@Override
	public void onLoginedEvent(String jid, String pwd) {
		// TODO Auto-generated method stub
		LocalEnv.mXmppEventNotifyManager.notifyAllListener(new LocalData(
				XmppEvent.XMPP_CONNECTION_ESTABLISHED_EVENT));
		MyToastUtil.showToastOnTop("登陆成功", 2000);
		LocalEnv.adapter.xmppSendPresence();
		LocalEnv.adapter.xmppSendPingRequest();
//		LocalEnv.adapter.xmppSendFeedBack();
		Mylog.d("发送出席");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.net.callback.XmppEventListener#onLogoutEvent()
	 */
	@Override
	public void onLogoutEvent() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAuthFailedEvent(String msg) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.net.callback.XmppEventListener#onConnectStateChangedEvent
	 * (int, int)
	 */
	@Override
	public void onConnectStateChangedEvent(XmppConnectionState newState,
			XmppConnectionState oldState) {
		// TODO Auto-generated method stub
		LocalData data = new LocalData(XmppEvent.XMPP_CONNECTION_LOST_EVENT);
		data.setNewConnectionState(newState);
		data.setLastConnectionState(oldState);
		LocalEnv.mXmppEventNotifyManager.notifyAllListener(data);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.net.callback.XmppEventListener#onUnkonwErrorEvent(java.lang
	 * .String)
	 */
	@Override
	public void onUnkonwErrorEvent(String errorMessage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConnectLostEvent(String reason, String conflictSid) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReceiveMessage(Message msg) {
		// TODO Auto-generated method stub
		Mylog.d("收到来自 " + msg.getFromNick() + " 的消息 " + msg.getBody());
		Message m = new Message(msg.getTo(), msg.getFrom());
		m.setType(Message.TEXT_MSG);
		m.setBody(msg.getBody());
		m.getMsgId();
		LocalEnv.adapter.xmppSendMessage(m);
	}

	@Override
	public void onReceiveMessageBackEvent(String from, String id) {
		// TODO Auto-generated method stub
		Mylog.d("收到来自 " + from + " 的消息回执。id为  " + id);
	}

	@Override
	public void onPresenceStatusEvent(String user, String show) {
		// TODO Auto-generated method stub
		Mylog.d("收到Presence状态变换 " + user + "  " + show);
	}

	@Override
	public void onGetPingPackageEvent(boolean c2s) {
		// TODO Auto-generated method stub
		if (c2s) {
			Mylog.d("服务器响应c2s心跳包");
		} else {
			Mylog.d("服务器发送s2c心跳包");
		}
	}

}
